import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import copy from 'rollup-plugin-copy';
import json from '@rollup/plugin-json';
import dts from 'rollup-plugin-dts';

import { babel } from '@rollup/plugin-babel';
import pkg from './package.json' with { type: 'json' };

// 更精确的外部依赖配置
const externalDependencies = [
  // 将 react 和 react-dom 作为外部依赖
  'react',
  'react-dom',
];

export default [
  // 构建 JavaScript 包
  {
    input: 'src/lib/index.ts',
    output: [
      {
        file: 'dist/index.cjs',
        format: 'cjs',
        sourcemap: process.env.NODE_ENV !== 'production',
        exports: 'named',
        inlineDynamicImports: true
      },
      {
        file: 'dist/index.js',
        format: 'es',
        sourcemap: process.env.NODE_ENV !== 'production',
        exports: 'named',
        inlineDynamicImports: true
      },
    ],
    plugins: [
      resolve({
        browser: true,
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
        preferBuiltins: false
      }),
      commonjs({
        // 确保 React 正确处理
        include: 'node_modules/**'
      }),
      json(), // 添加 JSON 插件
      typescript({
        tsconfig: './tsconfig.json',
        sourceMap: process.env.NODE_ENV !== 'production'
      }),
      babel({
        babelHelpers: 'bundled',
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
        presets: [
          ['@babel/preset-react', { runtime: 'automatic' }]
        ],
        exclude: 'node_modules/**'
      }),
      copy({
        targets: [
          { src: 'README.md', dest: 'dist' },
          {
            src: 'package.json', dest: 'dist', transform: (contents) => {
              const pkg = JSON.parse(contents.toString());
              delete pkg.dependencies;
              delete pkg.devDependencies;
              delete pkg.browserslist;
              delete pkg.eslintConfig;
              delete pkg.scripts;

              pkg.scripts = {
                pub: 'npm publish'
              };
              pkg.main = './index.cjs';
              pkg.module = './index.js';
              pkg.types = './index.d.ts';

              return JSON.stringify(pkg, null, 2);
            }
          }
        ]
      })
    ],
    external: id => {
      // 检查是否是外部依赖
      return externalDependencies.some(dep => {
        if (typeof dep === 'string') {
          return id === dep || id.startsWith(`${dep}/`);
        } else if (dep instanceof RegExp) {
          return dep.test(id);
        }
        return false;
      });
    }
  },
  // 构建类型定义文件
  {
    input: 'src/lib/index.ts',
    output: [{ file: pkg.types, format: 'es' }],
    plugins: [dts({
      compilerOptions: {
        incremental: false
      }
    })],
    external: id => {
      // 检查是否是外部依赖
      return externalDependencies.some(dep => {
        if (typeof dep === 'string') {
          return id === dep || id.startsWith(`${dep}/`);
        } else if (dep instanceof RegExp) {
          return dep.test(id);
        }
        return false;
      });
    },
  },
];